package com.whale.cloud.util.rest.schema;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whale.cloud.exception.CommonException;
import com.whale.cloud.util.StringUtils;
import com.whale.cloud.util.rest.condition.EColumnAuthority;
import com.whale.cloud.util.rest.query.GetSysConfigure;
import com.whale.cloud.util.rest.schema.T_SYS_COLUMN;
import com.whale.cloud.util.spring.SpringUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/** Created by paco on 6/6/2017. */
@Log4j2
@Component
@Scope("prototype")
public class FunctionImpl implements Function {
  private Column pkColumn;
  private T_SYS_FUNCTION table;
  private Function realTable;

  @Autowired private GetSysConfigure gsc;

  @Autowired private FunctionManager tableManager;

  @Override
  public void setFunction(T_SYS_FUNCTION t) {
    if (t == null) {
      throw new CommonException("-70", "初始化表时，表信息不能为空");
    }
    this.table = t;
  }

  @Override
  public long getId() {
    return table.getId();
  }

  @Override
  public String getDBName() {
    return table.getFunction_name();
  }

  @Override
  public String getShowName() {
    return table.getShow_name();
  }

  @Override
  public void initData() {}

  @Override
  public Column getColumnByDBName(String dbName) {
    if (StringUtils.isNull(dbName)) {
      throw new CommonException("-50", "字段名不能为空");
    }
    T_SYS_COLUMN column = null;
    JSONObject columnjo = gsc.getColumnByDBName(this.getId(), dbName);
    column = JSONObject.parseObject(columnjo.toJSONString(), T_SYS_COLUMN.class);
    Column c = SpringUtils.getBean(ColumnImpl.class);
    c.setFunction(this);
    c.setColumn(column);

    return c;
  }

  @Override
  public List<Column> getUserColumns(long userId, EColumnAuthority e) {
    List<Column> userColumns = new ArrayList<>();
    JSONArray userColumnsja = gsc.getAuthorityColumns(table.getId(), userId, e);
    if (userColumnsja != null && userColumnsja.size() > 0) {
      int cuLength = userColumnsja.size();
      JSONObject tempColumn = null;
      long columnId = 0;
      for (int i = 0; i < cuLength; i++) {
        tempColumn = userColumnsja.getJSONObject(i);
        if (tempColumn == null || tempColumn.isEmpty()) {
          log.error("-80", "user column index:" + i + " is null param:" + userColumnsja);
          continue;
        }

        columnId = tempColumn.getLongValue(getPK().getDb_name());
        T_SYS_COLUMN column = null;
        JSONObject columnjo = gsc.getColumnById(columnId);
        column = JSONObject.parseObject(columnjo.toJSONString(), T_SYS_COLUMN.class);
        Column c = SpringUtils.getBean(ColumnImpl.class);
        c.setFunction(this);
        c.setColumn(column);

        userColumns.add(c);
      }
    }

    return userColumns;
  }

  @Override
  public Column getPK() {
    if (pkColumn != null) {
      return pkColumn;
    }

    if (table.getPk_id() <= 0) {
      log.error("table:" + table.getFunction_name() + " not set pk column");
      throw new CommonException("-70", "表：" + table.getShow_name() + " 没有维护主键");
    }

    JSONObject columnjo = gsc.getColumnById(table.getPk_id());
    T_SYS_COLUMN column = JSONObject.parseObject(columnjo.toJSONString(), T_SYS_COLUMN.class);
    pkColumn = SpringUtils.getBean(ColumnImpl.class); //new ColumnImpl(this,column);
    pkColumn.setFunction(this);
    pkColumn.setColumn(column);

    return pkColumn;
  }

  @Override
  public String getRealFunctionName() {
    if (table.getSys_function_id() == 0) {
      return table.getFunction_name();
    } else {
      return getRealFunction().getDBName();
    }
  }

  @Override
  public Function getRealFunction() {
    if (realTable != null) {
      return realTable;
    }
    if (table.getSys_function_id() > 0) {
      realTable = tableManager.getById(table.getSys_function_id());
    }

    return realTable;
  }

  @Override
  public boolean getIsBc() {
    return "Y".equalsIgnoreCase(table.getIs_bc());
  }

  @Override
  public String getDataBCClassName() {
    return table.getBc_classname();
  }

  @Override
  public boolean getIsAc() {
    return "Y".equalsIgnoreCase(table.getIs_ac());
  }

  @Override
  public String getDataACClassName() {
    return table.getAc_classname();
  }

  @Override
  public boolean getIsBm() {
    return "Y".equalsIgnoreCase(table.getIs_bm());
  }

  @Override
  public String getDataBMClassName() {
    return table.getBm_classname();
  }

  @Override
  public boolean getIsAm() {
    return "Y".equalsIgnoreCase(table.getIs_am());
  }

  @Override
  public String getDataAMClassName() {
    return table.getAm_classname();
  }

  @Override
  public boolean getIsBd() {
    return "Y".equalsIgnoreCase(table.getIs_bd());
  }

  @Override
  public String getDataBDClassName() {
    return table.getBd_classname();
  }

  @Override
  public boolean getIsSubmit() {
    return "Y".equalsIgnoreCase(table.getIs_submit());
  }

  @Override
  public String getDataSubmitClassName() {
    return table.getSubmit_classname();
  }

  @Override
  public boolean getIsUnSubmit() {
    return "Y".equalsIgnoreCase(table.getIs_unsubmit());
  }

  @Override
  public String getUnSubmitClassName() {
    return table.getUnsummit_classname();
  }

  @Override
  public List<Column> getByDBNames(String dbNames) {
    List<Column> cs = new ArrayList<>();
    if (StringUtils.isNull(dbNames)) {
      return cs;
    }

    String[] columDBNames = dbNames.split("\\s*,\\s*");
    int clength = columDBNames.length;
    String tempColumnName = null;

    for (int i = 0; i < clength; i++) {
      tempColumnName = columDBNames[i];
      if (StringUtils.isNull(tempColumnName)) {
        continue;
      }
      Column c = getColumnByDBName(tempColumnName);
      if (c == null) {
        continue;
      }
      cs.add(c);
    }

    return cs;
  }

  @Override
  public String getDataFrom() {
    return table.getData_from();
  }
}
